#include "devio.hpp"
#include "sysapi.hpp"

#define LOG_TAG "driver_devio"
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h> 

void io_input_irq_call(void *args)
{
    robot_devio* thisp = static_cast<robot_devio*>(args);
}

/**
 * @brief Construct a new robot devio::robot devio object
 * @param  pin_num          My Param doc
 * @param  dir              My Param doc
 * @param  pu               My Param doc
 */
robot_devio::robot_devio(rt_base_t pin_num, Direct dir, Pull pu)
{
    pin_n = pin_num;
    if(dir==input && pu==high)
    {
        rt_pin_mode(pin_num, PIN_MODE_INPUT_PULLUP);
        rt_pin_attach_irq(pin_num, PIN_IRQ_MODE_FALLING, io_input_irq_call, this);
    }
    else if(dir==input && pu==low)
    {
        rt_pin_mode(pin_num, PIN_MODE_INPUT_PULLDOWN);
        rt_pin_attach_irq(pin_num, PIN_IRQ_MODE_RISING, io_input_irq_call, this);
    }
    else if(dir==output)
    {
        rt_pin_mode(pin_num, PIN_MODE_OUTPUT);
    }
}

/**
 * @brief Destroy the robot devio::robot devio object
 */
robot_devio::~robot_devio()
{
    rt_sem_detach(&irq_sem);

    rt_pin_detach_irq(pin_n);
    rt_pin_irq_enable(pin_n, PIN_IRQ_DISABLE);
}

/**
 * @brief 
 */
void robot_devio::open()
{
    rt_sem_init(&irq_sem, "io", 0, RT_IPC_FLAG_FIFO);
    rt_pin_irq_enable(pin_n, PIN_IRQ_ENABLE);
}



